const CardSpriteType = require('MJCommon').CardSpriteType;
var MJCardSprite = cc.Class({
    extends: cc.Component,
    properties: {
        m_cardFace: cc.Sprite,
        m_cardBack: cc.Sprite,
        _boChecked: false,
        _Card: 0,
        _map: null,
        _nodeY: -1,
        _btType: 0,
        _seadOrd: -1,
        _flag: true,
    },


    init: function (card, sead, map, btType) {
        this._boChecked = false;
        this._Card = card;
        this._seadOrd = sead;
        this.setCard(card);
        this._map = map;
        this._btType = btType;
        this.updateSprite();
        this.updateBackStyle();
    },

    copy: function () {
        var spr = new MJCardSprite();
        spr.init(this._Card, this._map, this._btType);
        spr.setChecked(false);
        return spr;
    },

    updateBackStyle: function (style) {
        if (style == null)
            style = 1;

        var file = 'bgFileName';
        if (this._Card == 0xf0) {
            file = 'backFileName';
        } else if (this._btType == CardSpriteType.cstCPGBack) {
            file = 'backFileName';
        }

        this.setBackFile(cc.find('Canvas').getComponent('MJScene').MJTexturePlist.getSpriteFrame(this._map.getMap(this._btType)[file]));
    },

    setFaceFile: function (fileName) {
        if (fileName.length == 0)
            return;
        this.m_cardFace.spriteFrame = fileName;
    },

    setBackFile: function (fileName) {
        if (fileName.length == 0)
            return;
        this.m_cardBack.spriteFrame = fileName;
        // this.setContentSize(this.m_cardBack.getContentSize());
    },

    updateOutPos: function (idx) {
        var fix = this._map.mOut.fix;
        var outColumn = this._map.outColumn;
        var zIndex = 100;
        if (this._seadOrd == 0) {
            zIndex = 200 - idx;
        } else if (this._seadOrd == 1) {
            zIndex -= idx;
        } else if (this._seadOrd == 2) {
            zIndex = 30 + idx;
        } else if (this._seadOrd == 3) {
            zIndex = 100 + idx;
        }

        var startPos = cc.p(this._map.mOut.startPos.x, this._map.mOut.startPos.y);
        if (idx >= outColumn * 5 - 20) {
            startPos = cc.pAdd(this._map.mOut.startPos, cc.pMult(this._map.mOut.fix2, 5));
            idx -= (outColumn * 5 - 20);
        } else if (idx >= outColumn * 4 - 12) {
            startPos = cc.pAdd(this._map.mOut.startPos, cc.pMult(this._map.mOut.fix2, 4));
            idx -= (outColumn * 4 - 12);
        } else if (idx >= outColumn * 3 - 6) {
            startPos = cc.pAdd(this._map.mOut.startPos, cc.pMult(this._map.mOut.fix2, 3));
            idx -= (outColumn * 3 - 6);
        } else if (idx >= outColumn * 2 - 2) {
            startPos = cc.pAdd(this._map.mOut.startPos, cc.pMult(this._map.mOut.fix2, 2));
            idx -= (outColumn * 2 - 2);
        } else if (idx >= outColumn) {
            startPos = cc.pAdd(this._map.mOut.startPos, this._map.mOut.fix2);
            idx -= outColumn;
        }

        startPos = cc.pAdd(startPos, cc.pMult(this._map.mOut.fix, idx));
        this.node.setPosition(startPos);
        this.node.zIndex = zIndex;
    },


    setColor: function (color) {
        if (this.m_cardBack)
            this.m_cardBack.node.setColor(color);
    },

    hideArrow: function () {
        if (this._flag) {
            this._flag = false;
            this.node.removeChildByTag(1233);
        }
    },

    setIsHaidi: function () {
        //是海底牌
        this.removeChildByTag(1233);
        var bg = Common.createViewByJson({
            fileName: 'img_flaghaidi.png',
            pos: '{-5,4}'
        });
        this.addChild(bg, 10);
        bg.setTag(1233);
    },

    showArrow: function (spriteFrame) {
        if (this._btType != CardSpriteType.cstOutCard) {
            return;
        }
        this._flag = true;
        this.node.removeChildByTag(1233);

        var arrowNode = new cc.Node();
        this.node.addChild(arrowNode, 10);

        var spr = arrowNode.addComponent(cc.Sprite)
        spr.spriteFrame = spriteFrame;
        arrowNode.setTag(1233);
        arrowNode.setScale(1 / this.node.getScale());
        arrowNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.1, cc.p(0, 10)), cc.moveBy(0.2, cc.p(0, -20)), cc.moveBy(0.1, cc.p(0, 10)))));
        arrowNode.y = this.node.height;
        // this.mFlag = true;
        // this.node.removeChildByTag(1233);
        // var arrow = Common.createViewByJson({
        //     fileName: 'img_cardflag.png',
        //     pos: '{0,160}'
        // });
        // this.addChild(arrow, 10);
        // arrow.setTag(1233);
        // arrow.setScale(1 / this.getScale());
        // arrow.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.1, cc.p(0, 10)), cc.moveBy(0.2, cc.p(0, -20)), cc.moveBy(0.1, cc.p(0, 10)))));
    },

    updateSprite: function () {
        this.node.setScale(this._map.getMap(this._btType).scale);
        this.m_cardFace.node.setPosition(this._map.getMap(this._btType).facePos);
        this.m_cardFace.node.setRotation(this._map.getMap(this._btType).faceRotate);
        this.m_cardFace.node.setScale(this._map.getMap(this._btType).faceScale.x, this._map.getMap(this._btType).faceScale.y);
    },

    isChecked: function () {
        return this._boChecked;
    },

    setChecked: function (value) {
        if (this.isChecked() == value) return;
        this._boChecked = value;
        if (this._nodeY == -1) {
            this._nodeY = this.node.y;
        }
        if (value) {
            // this.node.setColor(cc.color(164, 220, 255));
            this.node.y = this._nodeY + 25;
        } else {
            // this.node.setColor(cc.color(255, 255, 255));
            this.node.y = this._nodeY;
        }
        // this._boChecked = value;
        // if (this.m_btDir != 0)
        //     return;
        // if (value) {
        //     if (this._btType == CardSpriteType.cstOutCard)
        //         return;
        //     this.setPosition(this.m_BackPosition.x, this.m_BackPosition.y + 30);
        //     this.zIndex += 50;

        // } else {
        //     this.updateSprite();
        // }

    },

    touchAble: function () {
        if (this._btType == CardSpriteType.cstHandCard || this._btType == CardSpriteType.cstHandGet || this._btType == CardSpriteType.cstHandDown || this._btType == CardSpriteType.cstHandShow)
            return true;
        return false;
    },

    hitCard: function (btPos) {
        this.setChecked(false);
        if (btPos == null) {
            this._btType = CardSpriteType.cstOutCard; //刚打出去，在展示区
            this.updateSprite();
            this.updateBackStyle();
            this.updateOutPos(0);
        } else {
            this.hideHuHint();
            this._btType = CardSpriteType.cstOutCard; //刚打出去，在展示区 
            this.updateSprite();
            this.updateBackStyle();
            this.updateOutPos(btPos);

            // var card = new MJCardSprite(this.getCard(), this.m_btDir, this.m_btOrd, CardSpriteType.cstHandCard);
            // this.getParent().addChild(card);
            // card.runAction(cc.sequence(cc.scaleTo(0.1, 1.1), cc.delayTime(0.2), cc.fadeTo(0.1, 0), cc.removeSelf()));
        }
        this.setColor(cc.color(255, 255, 255))
    },

    hitToOut: function () {
        // this.hitCard();
    },


    hideHuHint: function () {
        if (this.mHintFlag) {
            this.mHintFlag = false;
            this.removeChildByTag(1234);
        }
    },


    isHintHu: function () {
        return this.mHintFlag;
    },

    showHuHint: function () {
        if (this._btType != CardSpriteType.cstHandShow && this._btType != CardSpriteType.cstHandGet && this._btType != CardSpriteType.cstHandDown) {
            return;
        }
        this.mHintFlag = true;

        this.removeChildByTag(1234);
        var arrow = Common.createViewByJson({
            fileName: 'icon_huhint.png',
            pos: '{0,150}'
        });
        this.addChild(arrow, 10);
        arrow.setTag(1234);
    },



    getCard: function () {
        return this._Card;
    },

    setCardBack: function () {
        this.m_cardFace.node.active = false;
        this.m_cardBack.node.active = true;
    },

    setCard: function (card) {
        this._Card = card;
        if (card == 0 || card == 0xff || card == 0xf0) {
            this.m_cardFace.node.active = false;
            this.setCardBack();
            return;
        }
        this.m_cardFace.node.active = true;
        var faceName = '';
        if (card == 35) {
            faceName = "mj_zhong";
        } else {
            var point = card % 10;
            switch (Number.parseInt(card / 10)) {
                case 0:
                    {
                        faceName = 'mj_wan' + point;
                    }
                    break;
                case 1:
                    {
                        faceName = 'mj_tiao' + point;
                    }
                    break;
                case 2:
                    {
                        faceName = 'mj_tong' + point;
                    }
                    break;
                default:
                    faceName = "";
                    break;
            }
        }

        if (faceName.length > 0) {
            this.m_cardFace.spriteFrame = cc.find('Canvas').getComponent('MJScene').MJTexturePlist.getSpriteFrame(faceName);
        } else {
            this.m_cardFace.node.active = false;
        }
    },

    setOpacity: function (opacity) {
        this.m_cardFace.setOpacity(opacity);
        this.m_cardBack.setOpacity(opacity);
    }
});